package com.example.car.controller;

import com.example.car.entity.Car;
import com.example.car.model.CarModel;
import com.example.car.params.CarParams;
import com.example.car.service.CarService;
import com.example.car.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "车辆接口")
@RestController
@RequestMapping("car")
public class CarController {

    @Autowired
    private CarService carService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @PostMapping("list")
    @ApiOperation("商品管理页面")
    public CommonResult getCarList(@RequestBody CarParams carParams) {

        List<Car> list = carService.getCarList(carParams);

        List<CarModel> collect = list.stream().map(item -> {
            CarModel carModel = new CarModel();
            BeanUtils.copyProperties(item, carModel);
            return carModel;
        }).collect(Collectors.toList());

        return CommonResult.success().data("data", collect);
    }

    @GetMapping("rentstandare/{rentType}/{carId}")
    @ApiOperation("返回车的租金标准")
    public CommonResult getCarList(@PathVariable("rentType") Integer rentType,
                                   @PathVariable("carId") String carId) {
        Double rentStandare = null;
        Car car = carService.getCarById(carId);
        if (car == null) {
            return CommonResult.error("该车辆不存在");
        }
        if (rentType == 1) {
            rentStandare = car.getRentStandardDay();
        }else {
            rentStandare = car.getRentStandardMouth();
        }
        if (rentStandare == null) {
            return CommonResult.error("系统异常");
        }

        return CommonResult.success().data("data", rentStandare);
    }


}
